<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <script>
    // 函数中this指向
    // 1.纯粹的函数 this指向window
    var name = "张三"
    function say() {
      console.log(this.name)
    }
    say()
    // setInterval(function () {
    //   console.log(this.name)
    // }, 2000)

    // arr.map(function () { })
    // 2.对象内部的函数  this指向上层对象
    var car = {
      name: "奔驰",
      run: function () {
        console.log(this.name)
      },
      room: {
        name: "高配",
        jia: function () {
          console.log(this.name)
        }
      }
    }
    car.run()
    //3. 构造函数中的this指向 指向当前实例化对象
    function Person(name, age) {
      this.name = name;
      this.age = age;
      this.say = function () {
        console.log(this.name)
      }
    }
    // new 一个对象的过程 也叫实例化的过程
    var xiaoli = new Person("小李", 18)
    xiaoli.say()
    var xiaowang = new Person("小王", 18)
    xiaowang.say()
    //  4.事件函数中的this指向 当前发生事件的元素
    // box.onclick = function () {
    //   // this----box
    // }
    for (var i = 0; i < lis.length; i++) {
      lis[i].onclick = function () {
        // this--当前点击的li
      }
    }
  </script>
</body>

</html>